package com.rock.upms.client.config;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.core.io.DefaultResourceLoader;
import com.google.common.collect.Maps;
import com.rock.common.util.PropertiesFileUtil;
import com.rock.common.util.StringUtil;

/**
 * upms web 配置文件
 * 
 * @author hotdog
 *
 */
public class Global {

	private static Global global = new Global();
	private static Map<String, String> map = Maps.newHashMap();
	public static final String SHOW = "1";
	public static final String HIDE = "0";
	public static final String YES = "1";
	public static final String NO = "0";
	public static final String TRUE = "true";
	public static final String FALSE = "false";
	public static final String TGT_KEY = "tgt";
	//会话key
	public static final String SHIRO_SESSIONID = "shiro-sessionid:";
	//全局会话key
	public static final String SSO_SERVER_SESSIONID = "sso-server-sessionid:";
	//全局会话key列表
	public static final String SSO_SERVER_SESSIONID_LIST = "sso-server-sessionid-list";
	//全局token
	public static final String SSO_TOKEN = "sso-token:";
	//局部会话key
	public static final String SSO_CLIENT_SESSIONID = "sso-client-sessionid:";
	//token 对应的所有局部会话集合
	public static final String SSO_TOKEN_CLIENT_SESSIONID_SET = "sso-token-client-sessionid-set:";

	public static Map<String, Object> dictMap = new ConcurrentHashMap();
	public static String ipAddress = "";
	public static final String WEB_CONFIG_PROPERTIES_NAME = "rockWeb";
	
	

	public static Global getInstance() {
		return global;
	}

	public static String getConfig(String key) {
		String value = (String) map.get(key);
		if (value == null) {
			value = PropertiesFileUtil.getInstance(WEB_CONFIG_PROPERTIES_NAME).get(key);
			map.put(key, value != null ? value : "");
		}
		return value;
	}

	// sys settings
	public static String getSysCode() {
		return getConfig("sys.code");
	}

	public static String getSysName() {
		try {
			return new String(getConfig("sys.name").getBytes("ISO-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String getSysCopyright() {
		return getConfig("sys.copyright");
	}

	public static String getSysVersion() {
		return getConfig("sys.version");
	}

	public static String getSysUrl() {
		return getConfig("sys.url");
	}
	
	
	public static String getSysSuccessUrl() {
		return getConfig("sys.success.url");
	}

	public static String getBasePath() {
		return getConfig("basePath");
	}

	public static String getAdminPath() {
		return getConfig("adminPath");
	}

	public static String getFrontPath() {
		return getConfig("frontPath");
	}

	// web
	public static String getWebViewPrefix() {
		return getConfig("web.view.prefix");
	}

	public static String getWebViewSuffix() {
		return getConfig("web.view.suffix");
	}

	public static String getWebSourcePath() {
		return getConfig("web.sourcePath");
	}

	public static String getWebMaxUploadSize() {
		return getConfig("web.maxUploadSize");
	}

	public static String getWebStaticFile() {
		return getConfig("web.staticFile");
	}
	public static String getWebUrlSuffix() {
		return getConfig("web.urlSuffix");
	}
	public static int getPageSize() {
		return StringUtil.toInteger(getConfig("page.size"));
	}

	public static int getPageNavigatePages() {
		return StringUtil.toInteger(getConfig("page.navigate.pages"));
	}
	
	
	// sso cas
	public static String getSSOClientType() {
		return getConfig("sso.client.type");
	}

	public static String getSSOServerUrl() {
		return getConfig("sso.server.url");
	}

	public static String getSSOType() {
		return getConfig("sso.type");
	}

	// session
	public static int getSessionTimeout() {
		return StringUtil.toInteger(getConfig("session.sessionTimeout"));
	}

	public static int getSessionTimeoutClean() {
		return StringUtil.toInteger(getConfig("session.sessionTimeoutClean"));
	}

	// redis
	public static String getRedisIp() {
		return getConfig("master.redis.ip");
	}

	public static int getRedisPort() {
		return StringUtil.toInteger(getConfig("master.redis.port"));
	}

	public static String getRedisPwd() {
		return getConfig("master.redis.password");
	}

	public static boolean getRedisTestOnBorrow() {
		if ("true".equals(getConfig("master.redis.testOnBorrow"))) {
			return true;
		}
		return false;
	}

	public static int getRedisMaxActive() {
		return StringUtil.toInteger(getConfig("master.redis.maxActive"));
	}

	public static int getRedisMaxIdle() {
		return StringUtil.toInteger(getConfig("master.redis.maxIdle"));
	}

	public static int getRedisWait() {
		return StringUtil.toInteger(getConfig("master.redis.maxWait"));
	}

	public static int getRedisTimeout() {
		return StringUtil.toInteger(getConfig("master.redis.timeout"));
	}

	public static Object getConst(String field) {
		try {
			return Global.class.getField(field).get(null);
		} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException localIllegalArgumentException) {
		}
		return null;
	}

	public static String getProjectPath() {
		String projectPath = getConfig("projectPath");
		if (StringUtil.isNotBlank(projectPath)) {
			return projectPath;
		}
		try {
			File file = new DefaultResourceLoader().getResource("").getFile();
			if (file != null) {
				for (;;) {
					File f = new File(file.getPath() + File.separator + "src" + File.separator + "main");
					if ((f == null) || (f.exists())) {
						break;
					}
					if (file.getParentFile() == null) {
						break;
					}
					file = file.getParentFile();
				}
				projectPath = file.toString();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return projectPath;
	}
}
